﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using DragDraw.Util;
using DragDraw.UserService;

namespace DragDraw
{
    public partial class WidgetControl :
        UserControl,
        IWidgetContainer
    {
        private static int m_zindex = 1;
        public WidgetControl()
        {
            InitializeComponent();
            TitleBar.Visibility = Visibility.Collapsed;
            BtnZoom.Visibility = Visibility.Collapsed;
            TitleBar.Cursor = Cursors.Hand;
            BtnZoom.Cursor = Cursors.Hand;
            Visibility = Visibility.Collapsed;
            if (OperationManager.Instance.IsInMySpace == false)
            {
                CanDrag = false;
                CanZoom = false;
            }
            else
            {
                CanDrag = true;
                CanZoom = true;
            }
            animHide.Completed += new EventHandler(delegate(object sender, EventArgs e)
                { this.Visibility = Visibility.Collapsed; });
        }
        private bool m_isShow = false;
        private UserControl m_widget;
        private bool m_isDirty = false;

        public bool IsDirty
        {
            get { return m_isDirty; }
            set { m_isDirty = value; }
        }
        public double PosX
        {
            get { return (double)this.GetValue(Canvas.LeftProperty); }
            set 
            {
                this.SetValue(Canvas.LeftProperty, value);
                m_isDirty = true;
            }
        }

        public double PosY
        {
            get { return (double)this.GetValue(Canvas.TopProperty); }
            set 
            {
                this.SetValue(Canvas.TopProperty, value);
                m_isDirty = true;
            }
        }

        public double SizeX
        {
            get { return m_widget.Width; }
            set
            {
                if (value > 0)
                {
                    this.Width = value;
                    m_widget.Width = value;
                    m_isDirty = true;
                }
            }
        }
        public double SizeY
        {
            get { return m_widget.Height; }
            set
            {
                if (value > 0)
                {
                    this.Height = value + TitleBar.Height;
                    m_widget.Height = value;
                    m_isDirty = true;
                }
            }
        }

        public int WidgetID { get; set; }

        public UserControl Widget
        {
            get { return m_widget; }
            set
            {
                m_widget = value;

                WidgetRoot.Children.Clear();
                this.SizeX = m_widget.Width;
                this.SizeY = m_widget.Height;
                WidgetRoot.Children.Add(m_widget);
            }
        }

        public string Title
        {
            get { return TxtTile.Text; }
            set { TxtTile.Text = value; }
        }

        public bool Show
        {
            get { return m_isShow; }
            set
            {
                if (!m_isShow && value)
                {
                    Visibility = Visibility.Visible;
                    animShow.Begin();
                }
                else if (m_isShow && !value)
                {
                    animHide.Begin();
                    
                }
                m_isShow = value;
            }
        }

        public bool CanDrag { get; set; }

        public bool CanZoom { get; set; }

        private bool trackingMouseMove = false;
        private Point mousePosition;

        private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!Show || !CanDrag) return;
            FrameworkElement element = sender as FrameworkElement;
            mousePosition = e.GetPosition(null);
            trackingMouseMove = true;
            if (null != element)
            {
                element.CaptureMouse();
            }
        }

        private void UserControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!Show || !CanDrag) return;
            FrameworkElement element = sender as FrameworkElement;
            trackingMouseMove = false;
            element.ReleaseMouseCapture();

            mousePosition.X = mousePosition.Y = 0;

            //update widget
            OperationManager.Instance.SetWidgetInfo(WidgetID, (int)PosX,
                        (int)PosY, (int)SizeX, (int)SizeY);
        }

        private void UserControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Show || !CanDrag) return;
            FrameworkElement element = sender as FrameworkElement;
            if (trackingMouseMove)
            {
                double deltaV = e.GetPosition(null).Y - mousePosition.Y;
                double deltaH = e.GetPosition(null).X - mousePosition.X;
                double newTop = deltaV + (double)this.GetValue(Canvas.TopProperty);
                double newLeft = deltaH + (double)this.GetValue(Canvas.LeftProperty);

                this.PosX = newLeft;
                this.PosY = newTop;

                mousePosition = e.GetPosition(null);
            }
        }

        private void WidgetRoot_MouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            //bring to front
            this.SetValue(Canvas.ZIndexProperty, m_zindex++);
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            DelToolDialogControl del = new DelToolDialogControl();
            del.WidgetID = this.WidgetID;
            OperationManager.Instance.
                CurrentSpaceManager.ShowModalDialog(del, "删除"+this.Title);
        }

        private void BtnZoom_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!Show || !CanZoom) return;
            FrameworkElement element = sender as FrameworkElement;
            mousePosition = e.GetPosition(null);
            trackingMouseMove = true;
            if (null != element)
            {
                element.CaptureMouse();
            }
        }

        private void BtnZoom_MouseMove(object sender, MouseEventArgs e)
        {
            if (!Show || !CanZoom) return;
            FrameworkElement element = sender as FrameworkElement;
            if (trackingMouseMove)
            {
                double deltaV = e.GetPosition(null).Y - mousePosition.Y;
                double deltaH = e.GetPosition(null).X - mousePosition.X;
                double newTop = deltaV + m_widget.Height;
                double newLeft = deltaH + m_widget.Width;

                if (newTop < 1 || newLeft < 1) // too small
                    return;

                this.SizeY = newTop;
                this.SizeX = newLeft;

                mousePosition = e.GetPosition(null);
            }
        }

        private void BtnZoom_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (!Show || !CanZoom) return;
            FrameworkElement element = sender as FrameworkElement;
            trackingMouseMove = false;
            element.ReleaseMouseCapture();

            mousePosition.X = mousePosition.Y = 0;
            //update info
            OperationManager.Instance.SetWidgetInfo(WidgetID, (int)PosX,
                        (int)PosY, (int)SizeX, (int)SizeY);
        }

        private void WidgetRoot_MouseEnter(object sender, MouseEventArgs e)
        {
            if(Show && CanDrag)
                TitleBar.Visibility = Visibility.Visible;
            if(Show && CanZoom)
                BtnZoom.Visibility = Visibility.Visible;
        }

        private void WidgetRoot_MouseLeave(object sender, MouseEventArgs e)
        {
            if (trackingMouseMove)
                return;
            TitleBar.Visibility = Visibility.Collapsed;
            BtnZoom.Visibility = Visibility.Collapsed;
        }
    }
}
